package com.handpay.zztong.hp.swiper.ic;

import java.security.Key;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;

public class SecureUtil
{
	private static final String AlogrithmKey3DES = "DESede";
	private static final String AlogrithmKeyDES = "DES";
	public static final String AlgorithmDESPKCS5 = "DES/ECB/PKCS5Padding";
	public static final String AlgorithmDESNo = "DES/ECB/NoPadding";
	public static final String Algorithm3DESPKCS5 = "DESede/ECB/PKCS5Padding";
	public static final String Algorithm3DESNo = "DESede/ECB/NoPadding";
	
	private static Key toKey3DES(byte[] key,String algorithm) throws Exception
	{  
		byte[] en_key = new byte[24];
		if (key.length == 16) {
			System.arraycopy(key, 0, en_key, 0, 16);
			System.arraycopy(key, 0, en_key, 16, 8);
		} else if(key.length == 8){
			System.arraycopy(key, 0, en_key, 0, 8);
			System.arraycopy(key, 0, en_key, 8, 8);
			System.arraycopy(key, 0, en_key, 16, 8);
		}else{
			en_key = key;
		}
        //实例化Des密钥  
        DESedeKeySpec dks = new DESedeKeySpec(en_key);
        //实例化密钥工厂  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);  
        //生成密钥  
        SecretKey secretKey=keyFactory.generateSecret(dks);  
        return secretKey;  
    }
	
	private static Key toKeyDES(byte[] key,String algorithm) throws Exception
	{  
        //实例化Des密钥  
        DESKeySpec dks = new DESKeySpec(key);
        //实例化密钥工厂  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);  
        //生成密钥  
        SecretKey secretKey=keyFactory.generateSecret(dks);  
        return secretKey;  
    }
	
	/**
	 * 3DES加密
	 * @param data
	 * @param key
	 * @return
	 */
	public static byte[] encode3DES(byte key[],byte data[],String algorithm) throws Exception
	{
        //还原密钥
        Key k = toKey3DES(key,AlogrithmKey3DES);
        //实例化
        Cipher cipher = Cipher.getInstance(algorithm);
        //初始化，设置为加密模式
        cipher.init(Cipher.ENCRYPT_MODE, k);
        //执行操作
        return cipher.doFinal(data);
	}
	
	public static String encode3DES(String key,String data,String algorithm) throws Exception
	{
		byte[] datas = data.getBytes();
		byte[] keys = key.getBytes();
		byte[] result = encode3DES(keys,datas,algorithm);
		return new String(result);
	}
	
	/**
	 * 3DES解密
	 * @param data
	 * @param key
	 * @return
	 */
	public static byte[] decode3DES(byte key[],byte data[],String algorithm) throws Exception
	{
		
        //还原密钥
        Key k = toKey3DES(key,AlogrithmKey3DES);
        //实例化
        Cipher cipher = Cipher.getInstance(algorithm);
        //初始化，设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, k);
        //执行操作
        return cipher.doFinal(data);
	}
	
	public static String decode3DES(String data,String key,String algorithm) throws Exception
	{
		byte[] datas = data.getBytes();
		byte[] keys = key.getBytes();
		byte[] result = decode3DES(datas,keys,algorithm);
		return new String(result);
	}
	
	/**
	 * DES加密
	 * @param data
	 * @param key
	 * @return
	 */
	public static byte[] encodeDES(byte key[],byte data[],String algorithm)
	{
		try
		{
			//还原密钥
			Key k = toKeyDES(key,AlogrithmKeyDES);
			//实例化
			Cipher cipher = Cipher.getInstance(algorithm);
			//初始化，设置为加密模式
			cipher.init(Cipher.ENCRYPT_MODE, k);
			//执行操作
			return cipher.doFinal(data,0,data.length);
		}
		catch(Exception e)
		{
			return null;
		}
	}
	
	/**
	 * DES解密
	 * @param data
	 * @param key
	 * @return
	 */
	public static byte[] decodeDES(byte key[],byte data[],String algorithm)
	{
		try
		{
			//还原密钥
			Key k = toKeyDES(key,AlogrithmKeyDES);
			//实例化
			Cipher cipher = Cipher.getInstance(algorithm);
			//初始化，设置为解密模式
			cipher.init(Cipher.DECRYPT_MODE, k);
			//执行操作
			return cipher.doFinal(data);
        }
		catch (Exception e)
		{  
            return null;
        }  
	}
	
	/**
	 * 获取随机数
	 * @param length
	 * @return
	 */
	public static byte[] getRandom(int length)
	{
		if (length < 1)
		{
			return null;
		}
		Random random = new Random();
		byte[] bytes = new byte[length];
		random.nextBytes(bytes);
		return bytes;
	}
}

